# -*- coding: utf-8 -*-
__author__ = 'zengao'
__email__ = '1759073141@qq.com'
"""
response响应处理
"""
import re
import pytest
from common.cache import cache
from common.regular import re, get_var
from utils.logger import logger


def check_results(r, validate):
    """检查运行结果"""
    expectcode = validate.get('expectcode')
    resultcheck = validate.get('resultcheck')
    regularcheck = validate.get('regularcheck')
    if expectcode:
        pytest.assume(expectcode == r.status_code)
    if resultcheck:
        pytest.assume(resultcheck in r.text)
    if regularcheck:
        pytest.assume(re.findall(regularcheck, r.text))


def get_result(r, extract):
    """获取值"""
    for key in extract:
        value = get_var(key, r.text)
        logger.debug("正则提取结果值：{}={}".format(key, value))
        cache.set(key, value)
        pytest.assume(key in cache)
